#region Summary

// 
// Owner : JackieHan 
// 
// Solution : DailyCollection 
// 
// Project : Jackie.Utility.Notify
// 
// File : SubscribeService.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 订阅接口的服务实现
// 
// History : 
//			2011-04-26 15:56:12	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using Common.Logging;
using Jackie.Utility.Notify.Contracts;
using Jackie.Utility.Notify.Domain;
using Jackie.Utility.Notify.Services.Manager;

#endregion

namespace Jackie.Utility.Notify.Services
{
    /// <summary> 
    /// 订阅接口的服务实现
    /// </summary>
    public class SubscribeService : ISubscribe
    {
        private readonly ILog logger;
        private readonly ClientManager clientManager;
        private readonly TitleManager titleManager;

        public SubscribeService()
        {
            logger = LogManager.GetLogger(GetType());
            clientManager = ClientManager.GetInstance();
            titleManager = TitleManager.GetInstance();
        }

        public void Subscribe(string clientSign, string title)
        {
            try
            {
                logger.Info(string.Format("start subsribe message!title:{0},client:{1}", title, clientSign));
                var callback = OperationContext.Current.GetCallbackChannel<IMessageCallback>();
                clientManager.SubscribeMessage(clientSign, title, callback);
                logger.Info("end subsribe message!");
            }
            catch (Exception ex)
            {
                logger.Error("error!", ex);
            }
        }

        public void Unsubscribe(string clientSign, string title)
        {
            try
            {
                logger.Info(string.Format("start unsubsribe message!title:{0},client:{1}", title, clientSign));
                clientManager.UnsubscribeMessage(clientSign, title);
                logger.Info("end unsubsribe message!");
            }
            catch (Exception ex)
            {
                logger.Error("error!", ex);
            }
        }

        public TitleInfo Get(string titleCode)
        {
            logger.Info("start get message title info by code:" + titleCode);
            var info = titleManager.GetTitle(titleCode);
            logger.Info("end get message title info");
            return info;
        }
    }
}